﻿using MediatR;
using SheepCultivation.Common.Domain.Week2;
using SheepCultivation.Common.Dto;
using SheepCultivation.Common.Infrastructure.Interface;
using SheepCultivation.Enum;

namespace SheepCultivation.API.AddliCation.BuildingD
{
    public class BuildingSeachHandler : IRequestHandler<BuildingSeachCommon, Result>
    {
        #region 仓储
        /// <summary>
        /// 仓储
        /// </summary>
        public readonly IBaseRepository<Building> BuildingRepository;
        public readonly IBaseRepository<BuildingType> BuildingTypeRepository;
        public readonly ILogger<BuildingSeachHandler> logger;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="buildingRepository">栋舍信息</param>
        /// <param name="buildingTypeRepository">栋舍类型</param>
        /// <param name="logger">日志</param>
        public BuildingSeachHandler(IBaseRepository<Building> buildingRepository, IBaseRepository<BuildingType> buildingTypeRepository, ILogger<BuildingSeachHandler> logger)
        {
            BuildingRepository = buildingRepository;
            BuildingTypeRepository = buildingTypeRepository;
            this.logger = logger;
        }
        #endregion

        #region 栋舍信息 显示
        /// <summary>
        /// 栋舍信息显示
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Result> Handle(BuildingSeachCommon request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonEnumCode.error };
            //异常处理
            try
            {
                logger.LogInformation("栋舍信息显示成功!");
                var data = new pageDto() { };
                var list = (from a in BuildingRepository.GetAll()
                            join b in BuildingTypeRepository.GetAll() on a.BuildingTypeId equals b.BuildingTypeId
                            select new BuildingDto()
                            {
                                BuildingId = a.BuildingId,
                                BuildingName = a.BuildingName,
                                BuildingPerson = a.BuildingPerson,
                                BuildingStaff = a.BuildingStaff,
                                BuildingState = a.BuildingState,
                                BuildingRenamk = a.BuildingRenamk,
                                BuildingTypeId = b.BuildingTypeId,
                                BuildingTypeName = b.BuildingTypeName,
                            }).ToList();

                //查询条件：栋舍类型Id 栋舍名称 负责人
                if (request.buildingTypeId != 0)
                {
                    list = list.Where(x => x.BuildingTypeId == request.buildingTypeId).ToList();
                }
                if (!string.IsNullOrEmpty(request.buildingName))
                {
                    list = list.Where(x => x.BuildingName.Contains(request.buildingName)).ToList();
                }
                if (!string.IsNullOrEmpty(request.buildingPerson))
                {
                    list = list.Where(x => x.BuildingPerson.Contains(request.buildingPerson)).ToList();
                }

                //分页
                data.totalCount = list.Count();
                data.pageCount = (int)Math.Ceiling((list.Count() * 1.0) / request.pageSize);
                data.data = list.OrderBy(x => x.BuildingId).Skip((request.pageIndex - 1) * request.pageSize).Take(request.pageSize).ToList();

                result.Code = (int)CommonEnumCode.success;
                result.Data = data;
                result.Msg = "栋舍信息显示成功";
            }
            catch (Exception ex)
            {
                logger.LogError("栋舍信息显示失败!" + ex.Message);
                result.Msg = "栋舍信息显示失败";
            }
            return result;
        }
        #endregion
    }

    /// <summary>
    /// 分页
    /// </summary>
    public class pageDto
    {
        public int totalCount { get; set; }
        public int pageCount { get; set; }
        public List<BuildingDto> data { get; set; }
    }

    /// <summary>
    /// 栋舍信息Dto
    /// </summary>
    public class BuildingDto
    {
        /// <summary>
        /// 栋舍Id
        /// </summary>
        public int BuildingId { get; set; }

        /// <summary>
        /// 栋舍类型Id（栋舍类型表 BuildingType）
        /// </summary>
        public int BuildingTypeId { get; set; }

        /// <summary>
        /// 栋舍名称
        /// </summary>
        public string? BuildingName { get; set; }
        /// <summary>
        /// 负责人
        /// </summary>
        public string? BuildingPerson { get; set; }
        /// <summary>
        /// 生产人员
        /// </summary>
        public string? BuildingStaff { get; set; }
        /// <summary>
        /// 状态 1：启用 2：禁用
        /// </summary>
        public int BuildingState { get; set; }
        /// <summary>
        /// 备注
        /// </summary>
        public string? BuildingRenamk { get; set; }
        /// <summary>
        /// 栋舍类型名称
        /// </summary>
        public string? BuildingTypeName { get; set; }
    }

}
